{% set prefix = DEFAULT_CONTAINER_REGISTRY %}
FROM {{ prefix }}ubuntu:20.04

{% set legacy = LEGACY_SONIC_MGMT_DOCKER %}

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y apt-transport-https \
                                         apt-utils \
                                         build-essential \
                                         ca-certificates \
                                         cmake \
                                         curl \
                                         default-jre \
                                         git \
                                         gnupg \
                                         gnupg-agent \
                                         iproute2 \
                                         iputils-ping \
                                         isc-dhcp-client \
                                         libffi-dev \
                                         libssl-dev \
                                         libxml2 \
                                         libxslt1-dev \
                                         lsb-release \
                                         jq \
                                         openssh-server \
                                         psmisc \
                                         python \
                                         python-dev \
                                         python3-pip \
                                         python3-venv \
                                         rsyslog \
                                         shellcheck \
                                         snmp \
                                         software-properties-common \
                                         sshpass \
                                         sudo \
                                         tcpdump \
                                         telnet \
                                         vim

RUN python3 -m pip install --upgrade pip setuptools wheel
RUN python3 -m pip install aiohttp \
                 allure-pytest==2.8.22 \
                 ansible==6.7.0 \
                 azure-storage-blob==12.9.0 \
                 azure-kusto-data \
                 azure-kusto-ingest \
                 defusedxml \
                 celery[redis]==5.2.7 \
                 cffi \
                 contextlib2==0.6.0.post1 \
                 cryptography==41.0.2 \
                 ctypesgen \
                 dpkt \
                 dpugen==0.1.1 \
                 "future>=0.16.0" \
                 gitpython \
                 ipaddr \
                 ipython==8.12.2 \
                 ixload \
                 ixnetwork-restpy==1.0.64 \
                 ixnetwork-open-traffic-generator==0.0.79 \
                 jinja2==3.1.2 \
                 jsonpatch \
                 lxml \
                 markupsafe==2.0.1 \
                 mock \
                 msrest==0.6.21 \
                 natsort \
                 ncclient \
                 netaddr \
                 netmiko==2.4.2 \
                 pandas \
                 paramiko==2.9.5 \
                 passlib \
                 pexpect \
                 prettytable \
                 psutil \
                 ptf \
                 pyasn1==0.4.8 \
                 pycryptodome==3.9.8 \
                 pyfiglet \
                 pylint==1.8.1 \
                 pyro4 \
                 pysnmp==4.4.12 \
                 pysubnettree \
                 pytest-ansible==4.0.0 \
                 pytest-html \
                 pytest-repeat \
                 pytest-xdist==1.28.0 \
                 python-dateutil \
                 pytest==7.4.0 \
                 PyYAML \
                 redis \
                 requests \
                 retry \
                 rpyc \
                 scandir \
                 scapy==2.5.0 \
                 setuptools-rust \
                 six \
                 snappi==0.9.1 \
                 snappi-convergence==0.4.1 \
                 snappi-ixnetwork==0.9.1 \
                 tabulate \
                 textfsm==1.1.2 \
                 thrift==0.11.0 \
                 virtualenv \
    && wget https://github.com/nanomsg/nanomsg/archive/1.2.tar.gz \
    && tar xvfz 1.2.tar.gz \
    && cd nanomsg-1.2      \
    && mkdir -p build      \
    && cd build            \
    && cmake ..            \
    && make install        \
    && ldconfig            \
    && cd ../..            \
    && rm -fr nanomsg-1.2  \
    && rm -f 1.2.tar.gz  \
    && python3 -m pip install nnpy

{% if legacy == 'y' or legacy == '1' %}
RUN curl -fsSL https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py \
    && python2 get-pip.py \
    && rm -f get-pip.py

RUN python2 -m pip install --upgrade pip setuptools wheel
RUN python2 -m pip install allure-pytest==2.8.22 \
                ansible==2.8.20 \
                azure-storage-blob==12.9.0 \
                celery[redis]==4.4.7 \
                cffi==1.12.0 \
                contextlib2==0.6.0.post1 \
                cryptography==3.3.2 \
                ctypesgen \
                dpkt \
                dpugen==0.1.1 \
                "future>=0.16.0" \
                gitpython \
                ipaddr \
                ipython==5.4.1 \
                ixload \
                ixnetwork-restpy==1.0.64 \
                ixnetwork-open-traffic-generator==0.0.79 \
                jinja2==2.10.1 \
                jsonpatch \
                lazy-object-proxy==1.6.0 \
                lxml \
                mock \
                msrest==0.6.21 \
                natsort \
                netaddr==0.10.1 \
                netmiko==2.4.2 \
                nnpy \
                pandas \
                paramiko==2.7.2 \
                passlib \
                pexpect \
                prettytable \
                protobuf==3.15.0 \
                psutil \
                ptf \
                pyaml==21.10.1 \
                pyasn1==0.1.9 \
                pycryptodome==3.9.8 \
                pyfiglet \
                pylint==1.8.1 \
                pyro4 \
                pysnmp==4.2.5 \
                pysubnettree \
                pytest==4.6.11 \
                pytest-ansible \
                pytest-repeat \
                pytest-html \
                pytest-xdist==1.28.0 \
                python-dateutil \
                PyYAML \
                redis \
                requests \
                retry \
                rpyc \
                scandir \
                scapy==2.5.0 \
                six \
                snappi==0.9.1 \
                snappi-convergence==0.4.1 \
                snappi-ixnetwork==0.9.1 \
                statistics \
                tabulate \
                textfsm==1.1.3 \
                thrift==0.11.0 \
                virtualenv
{% endif %}

# Install docker-ce-cli
RUN curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -                                        \
    && add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"    \
    && apt-get update                                                                                              \
    && apt-get install -y docker-ce-cli

# Install Azure CLI, following https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-linux?pivots=apt#option-2-step-by-step-installation-instructions
RUN mkdir -p /etc/apt/keyrings \
    && curl -sLS https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | tee /etc/apt/keyrings/microsoft.gpg > /dev/null \
    && chmod go+r /etc/apt/keyrings/microsoft.gpg \
    && echo "deb [arch=`dpkg --print-architecture` signed-by=/etc/apt/keyrings/microsoft.gpg] https://packages.microsoft.com/repos/azure-cli/ `lsb_release -cs` main" | tee /etc/apt/sources.list.d/azure-cli.list \
    && apt-get update && apt-get install -y azure-cli

## Copy and install sonic-mgmt docker dependencies
COPY debs/sonic-device-data_*.deb debs/

RUN dpkg -i debs/sonic-device-data_*.deb

# Install protobuf 3.21.12 which is from https://deb.debian.org/debian/pool/main/p/protobuf/protobuf_3.21.12-3.dsc
RUN mkdir -p /tmp/protobuf \
    && cd /tmp/protobuf \
    && wget https://sonicstorage.blob.core.windows.net/public/ubuntu/20.04/libprotobuf-dev_3.21.12-3_amd64.deb \
    && wget https://sonicstorage.blob.core.windows.net/public/ubuntu/20.04/libprotobuf-lite32_3.21.12-3_amd64.deb \
    && wget https://sonicstorage.blob.core.windows.net/public/ubuntu/20.04/libprotobuf32_3.21.12-3_amd64.deb \
    && wget https://sonicstorage.blob.core.windows.net/public/ubuntu/20.04/libprotoc-dev_3.21.12-3_amd64.deb \
    && wget https://sonicstorage.blob.core.windows.net/public/ubuntu/20.04/libprotoc32_3.21.12-3_amd64.deb \
    && wget https://sonicstorage.blob.core.windows.net/public/ubuntu/20.04/protobuf-compiler_3.21.12-3_amd64.deb \
    && wget https://sonicstorage.blob.core.windows.net/public/ubuntu/20.04/python3-protobuf_3.21.12-3_amd64.deb \
    && dpkg -i *.deb \
    && rm -rf /tmp/protobuf

# Install dash-api
RUN cd /tmp \
    && mkdir -p /usr/lib/python3/dist-packages/dash_api \
    && wget https://raw.githubusercontent.com/sonic-net/sonic-dash-api/master/misc/pypkg/dash_api/__init__.py -O /usr/lib/python3/dist-packages/dash_api/__init__.py \
    && git clone https://github.com/sonic-net/sonic-dash-api.git \
    && protoc -I=sonic-dash-api/proto --python_out=/usr/lib/python3/dist-packages/dash_api sonic-dash-api/proto/*.proto \
    && rm -rf /tmp/sonic-dash-api

RUN mkdir /var/run/sshd
EXPOSE 22

# Add user
ARG user
ARG uid
ARG guid
ARG hostname

ENV BUILD_HOSTNAME $hostname
ENV USER $user
ENV CC=gcc CPP=cpp CXX=c++ LDSHARED="gcc -pthread -shared" PYMSSQL_BUILD_WITH_BUNDLED_FREETDS=1

RUN groupadd -f -r -g $guid g$user

RUN useradd $user -l -u $uid -g $guid -d /var/$user -m -s /bin/bash

COPY sonic-jenkins.pub /var/$user/.ssh/authorized_keys2

RUN echo "Host *\n\tStrictHostKeyChecking no\n" > /var/$user/.ssh/config
RUN chown $user /var/$user/.ssh -R
RUN chmod go= /var/$user/.ssh -R

# Add user to sudoers
RUN echo "$user ALL=(ALL) NOPASSWD:ALL" >>/etc/sudoers

USER $user
WORKDIR /var/$user

# Add az symlink for backwards compatibility
RUN mkdir bin && ln -s /usr/bin/az bin/az

USER root
WORKDIR /azp
COPY start.sh \
     0001-Fix-getattr-AttributeError-in-multi-thread-scenario.patch \
     ./
RUN chmod +x start.sh \
    && ln -sf /usr/bin/python3 /usr/bin/python \
    && ln -sf `which pip3` /usr/bin/pip \
    && ln -sf `which pip3` /usr/local/sbin/pip \
    && patch -u -b /usr/local/lib/python3.8/dist-packages/ansible/plugins/loader.py -i /azp/0001-Fix-getattr-AttributeError-in-multi-thread-scenario.patch

USER $user
WORKDIR /var/$user
